<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { formItem } from '../form-item'
import formBox from '@/components/queryTable/form/index.vue'
import tableBox from '@/components/queryTable/table/index.vue'
import { TableItem } from '@/api/common'
import ajjcdService from '@/api/ajjcsd'

const formLabel = ref()
const loading = ref()
// 控制更多条件显示隐藏

// 生命周期
onMounted(async () => {
  // 查询列表数据
  loading.value = true
  formLabel.value = await formItem('ajsdjc')
  await init()
  loading.value = false
})

const init = async () => {
  // 获取权限数据
  //   queryDlrsjqx()
  changePageNum(1)
}

// watch(
//   () => [props.optionsObj.cbbmbmList, props.optionsObj.cbdwbmList],
//   (val) => {
//     form.value.filter.cbdwbmList.push(val[1][0]?.dm)
//     form.value.filter.cbbmbmList = val[0].map((bm: any) => bm.dm)
//     changePageNum(1)
//   },
// )

// watch(
//   () => form.value.filter.cbdwbmList,
//   (val) => {
//     console.log(val,'val')
//   },
// )

// 更多条件查询
const form = ref<any>({
  pageSize: 20,
  pageNum: 1,
  filter: {
    ajmc: '',
    bmsah: '',
    tysah: '',
  },
})

// 表格数据
const tableData = ref<TableItem>({
  data: [],
  titles: [
    {
      label: '案件名称',
      prop: 'ajmc',
      width: 300,
      custom: false,
      sort: true,
      dqfs: 'left',
    },
    { label: '部门受案号', prop: 'bmsah', width: 360, dqfs: 'left' },
    { label: '统一受案号', prop: 'tysah', width: null, dqfs: 'left' },
    { label: '检察官', prop: 'cbjcg', width: null, dqfs: 'left' },
    { label: '锁定人', prop: 'sdrxm', width: null, dqfs: 'left' },
    { label: '锁定时间', prop: 'sdrq', width: null, dqfs: 'left' },
  ],
})

const total = ref<number>(0)
const page = computed(() => {
  return {
    pageNum: form.value.pageNum,
    pageSize: form.value.pageSize,
    total: total.value,
  }
})

// 页码发生改变时
const changePageNum = (val: number) => {
  form.value.pageNum = val
  queryTable()
}

// 页条数发生改变时
const changeSize = (val: number) => {
  form.value.pageNum = 1
  form.value.pageSize = val
  queryTable()
}

/*
 * 查询列表数据
 */
const queryTable = (refresh?: boolean) => {
  const params = { ...form.value }
  loading.value = true
  ajjcdService
    .queryAjsdjl(params)
    .then((res) => {
      tableData.value.data = res.data.list
      total.value = res.data.total
      if (refresh) ElMessage({ type: 'success', message: '刷新成功' })
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
    .finally(() => {
      loading.value = false
    })
}

// 子组件传过来的方法
const searchTable = (data: any) => {
  let newObj: any = {}
  for (const key in data) {
    if (key.endsWith('-rq')) {
      const kssj = key.replace('-rq', 'ksrq')
      const jssj = key.replace('-rq', 'jsrq')
      if (data[key]?.length) {
        newObj[kssj] = data[key][0]
        newObj[jssj] = data[key][1]
      } else {
        newObj[kssj] = ''
        newObj[jssj] = ''
      }
    }
    newObj[key] = data[key]
  }
  form.value.filter = newObj
  changePageNum(1)
}

/**
 * 表格选择
 */
const multipleSelection = ref<any[]>([])
const selectionChange = (val: any[]) => {
  multipleSelection.value = val
}

const tableBoxRef = ref<InstanceType<typeof tableBox>>()
const rowClick = (row: any) => {
  if (multipleSelection.value?.includes(row)) {
    tableBoxRef.value?.refTable.toggleRowSelection(row, false)
  } else {
    tableBoxRef.value?.refTable.toggleRowSelection(row, true)
  }
}

// 解锁
const unlockAj = () => {
  const params = multipleSelection.value.map((item) => {
    return {
      ajbh: item.ajbh,
      bmsah: item.bmsah,
    }
  })
  ajjcdService
    .queryUnlockAj(params)
    .then(() => {
      ElMessage({ type: 'success', message: '解锁成功' })
      queryTable()
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
}

const refresh = () => {
  queryTable(true)
}
</script>
<template>
  <div class="container">
    <div class="form">
      <form-box
        :formData="form.filter"
        :formLabel="formLabel"
        :moreShow="false"
        @searchEvent="searchTable"
      ></form-box>
    </div>

    <div class="submit-list">
      <div>
        <button class="btn_operation" @click="unlockAj" :disabled="!multipleSelection.length">
          <div class="btn-password"></div>
          解锁
        </button>
        <button class="btn_operation" @click="refresh">
          <div class="btn-refresh"></div>
          刷新
        </button>
      </div>
    </div>

    <div class="table">
      <table-box
        gnbm="cqyj"
        ref="tableBoxRef"
        :tableData="tableData"
        :selectShow="true"
        :showExport="false"
        :changeHeader="false"
        :getTableDataParam="page"
        :formParams="form"
        @selectionChange="selectionChange"
        @changeCurrent="changePageNum"
        @changeSize="changeSize"
        @rowClick="rowClick"
      >
      </table-box>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex !important;
  flex-direction: column;

  .submit-list {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-bottom: 10px;
  }

  .table {
    flex: 1;
    flex-shrink: 0;
    overflow: auto;
  }

  .active-img {
    float: left;
    cursor: pointer;
  }
  .active-img + .active-img {
    margin-left: 10px;
  }
}
</style>
